﻿using HlELock.Core;
using HlELock.Core.Events;
using HlELock.Domain;
using HlELock.Domain.Entities;
using HlELock.Modules.Hotel.Main.Views;
using Microsoft.EntityFrameworkCore;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace HlELock.Modules.Hotel.Main.ViewModels
{
    internal class HotelMainViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;

        public HotelMainViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            TopMenuSource = new ObservableCollection<Menu>
            {
                //new Menu ("数据中心"),
               // new Menu ("前台管理"),
                //new Menu ("门锁管理",nameof(LockMenu)),
                //new Menu ("卡片管理"),
               // new Menu (AppHelper.GetResourceString("KeFangGuanLi") ,nameof(RoomManagementMenu)),
                new Menu (AppHelper.GetResourceString("GuiBingJieDai"),nameof(CheckInMenu)),
             //  new Menu (AppHelper.GetResourceString("YongHuGuanLi"),nameof(UserManagementPage))
                //new Menu ("维修申报"),
                //new Menu ("投诉处理"),
            };
            RefreshRoomList();
            EventBus.RefreshRoomListAction(RefreshRoomList);
        }
        private void RefreshRoomList()
        {
            using (var context = new HlELockDbContext())
            {
                var sql = "update rooms set IsOccupancies=1 where rooms.RoomNumber in (select roomnumber from checkinrooms where checkouttime is null)";
                context.ExecuteSqlRaw(sql);
                sql = "update rooms set IsOccupancies=0 where rooms.RoomNumber not in (select roomnumber from checkinrooms where checkouttime is null)";
                var rooms = context.Rooms.ToObservableCollection();
                RoomList = rooms.GroupBy(g => g.FloorNumber).Select(s => new RoomList
                {
                    Floor = s.Key + "楼",
                    Rooms = s.ToObservableCollection()
                }).ToObservableCollection();
            }
        }

        private ObservableCollection<RoomList> _roomList;
        public ObservableCollection<RoomList> RoomList
        {
            get { return _roomList; }
            set { SetProperty(ref _roomList, value); }
        }
        public ObservableCollection<Menu> TopMenuSource { get; set; }

        private Menu _currentMenu;
        public Menu CurrentMenu
        {
            get { return _currentMenu; }
            set
            {
                SetProperty(ref _currentMenu, value);
                if (!string.IsNullOrWhiteSpace(_currentMenu.Uri))
                {
                    _regionManager.RequestNavigate(RegionNames.HotelMainRibbon, _currentMenu.Uri);
                }
            }
        }
        private DelegateCommand _loadedCommand;
        public DelegateCommand LoadedCommand =>
            _loadedCommand ?? (_loadedCommand = new DelegateCommand(ExecuteLoadedCommand));

        void ExecuteLoadedCommand()
        { 
            // 初始化工作
            if (MyAppContext.IsSf)
            {
                MyAppContext.CardService = new SfCardService();
            }
            else
            {
                MyAppContext.CardService = new MyCardService();
                //初始化usb设备
                var title = AppHelper.GetResourceString("SheBeiPeiZhi");
                EventBus.Instace.GetEvent<DialogWindowEvent>().Publish(new Core.Events.DialogWindowEventArgs
                {
                    Title = title,
                    Uri = "SearchHotelDeviceControl"
                });
            }

            CurrentMenu = TopMenuSource.First(f => f.Name == AppHelper.GetResourceString("GuiBingJieDai"));


        }

        private DelegateCommand<Room> _roomClickCommand;
        public DelegateCommand<Room> RoomClickCommand =>
            _roomClickCommand ?? (_roomClickCommand = new DelegateCommand<Room>(ExecuteRoomClickCommand));

        void ExecuteRoomClickCommand(Room obj)
        {
            if (obj.IsOccupancies)
            {
                EventBus.ShowDialogWindow(new DialogWindowEventArgs
                {
                    Parameters = new System.Collections.Generic.Dictionary<string, object> { { nameof(Room), obj } },
                    Title = AppHelper.GetResourceString("FangJianXinXi"),
                    Uri = nameof(RoomInfoUserControl)
                });
            }
            else
            {
                EventBus.Instace.GetEvent<DialogWindowEvent>().Publish(new Core.Events.DialogWindowEventArgs
                {
                    Title = AppHelper.GetResourceString("BanLiRuZhu"),
                    Uri = nameof(CheckInPage)
                });
            }

        }
    }
    internal class Menu
    {
        public Menu(string name, string uri = null)
        {
            Name = name;
            Uri = uri;
        }
        public string Name { get; set; }
        public string Uri { get; set; }
    }

    internal class RoomList : BindableBase
    {
        public string Floor { get; set; }
        public ObservableCollection<Room> Rooms { get; set; }
    }
}
